package cn.iocoder.yudao.module.system.controller.admin.tips;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.tips.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tips.TipsDO;
import cn.iocoder.yudao.module.system.service.tips.TipsService;

@Tag(name = "管理后台 - 提示内容")
@RestController
@RequestMapping("/system/tips")
@Validated
public class TipsController {

    @Resource
    private TipsService tipsService;

    @PostMapping("/create")
    @Operation(summary = "创建提示内容")
    @PreAuthorize("@ss.hasPermission('system:tips:create')")
    public CommonResult<Long> createTips(@Valid @RequestBody TipsSaveReqVO createReqVO) {
        return success(tipsService.createTips(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提示内容")
    @PreAuthorize("@ss.hasPermission('system:tips:update')")
    public CommonResult<Boolean> updateTips(@Valid @RequestBody TipsSaveReqVO updateReqVO) {
        tipsService.updateTips(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提示内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tips:delete')")
    public CommonResult<Boolean> deleteTips(@RequestParam("id") Long id) {
        tipsService.deleteTips(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提示内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tips:query')")
    public CommonResult<TipsRespVO> getTips(@RequestParam("id") Long id) {
        TipsDO tips = tipsService.getTips(id);
        return success(BeanUtils.toBean(tips, TipsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提示内容分页")
    @PreAuthorize("@ss.hasPermission('system:tips:query')")
    public CommonResult<PageResult<TipsRespVO>> getTipsPage(@Valid TipsPageReqVO pageReqVO) {
        PageResult<TipsDO> pageResult = tipsService.getTipsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TipsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提示内容 Excel")
    @PreAuthorize("@ss.hasPermission('system:tips:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTipsExcel(@Valid TipsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TipsDO> list = tipsService.getTipsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "提示内容.xls", "数据", TipsRespVO.class,
                        BeanUtils.toBean(list, TipsRespVO.class));
    }

}